package com.wuwangfu.window;

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;

/**
 * @Author jcshen
 * @Date 2023-02-23
 * @PackageName:com.wuwangfu.window
 * @ClassName: ProcessTimeTumbleWindowAll
 * @Description:
 * @Version 1.0.0
 *
 * 不分组，按照processingTime划分滚动窗口
 *
 */
public class ProcessTimeTumbleWindowAll {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //旧API想要指定时间类型，新API不需要指定
//        env.setStreamTimeCharacteristic(TimeCharacteristic.ProcessingTime);

        DataStreamSource<String> line = env.socketTextStream("localhost", 8888);
        SingleOutputStreamOperator<Integer> maped = line.map(Integer::parseInt);
        /*不keyBy，只有1个并行度，按所在机器的processingTime划分窗口（通过web ui可以看出）*/
        //默认是ProcessingTime（处理时间，当前机器时间），TumblingProcessingTimeWindows（滚动窗口）
//        AllWindowedStream<Integer, TimeWindow> windowed = maped.timeWindowAll(Time.seconds(5));
//        windowed.sum(0).print();
        /*-------------------------新API---------------------------------*/
        /**
         * ctrl+alt 查看 WindowAssigner
         */
        maped.windowAll(TumblingProcessingTimeWindows.of(Time.seconds(5)))
                        .sum(0)
                                .print();

        https://www.bilibili.com/video/BV1Tt4y167AD?p=59&spm_id_from=pageDriver&vd_source=7c7fabc8d8dc017cd2c5cd6d38964eb2
        env.execute();
    }
}
